package com.xiaotu.spider.guduo.pageprocessor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xiaotu.common.model.MaoYanMovieBasicInfoModel;
import com.xiaotu.common.model.TVDataSourceRelatetionModel;
import com.xiaotu.common.model.TvInfoSpiderTempModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.HtmlUtil;
import com.xiaotu.common.util.KeywordEncode;
import com.xiaotu.common.util.MathUtil;
import com.xiaotu.common.util.PageUtils;
import com.xiaotu.common.util.StringUtil;
import com.xiaotu.server.service.guduo.SupplementGuDuoRecordService;
import com.xiaotu.server.service.maoyan.MaoYanMovieBasicInfoService;
import com.xiaotu.server.service.relation.TVDataSourceRelationService;
import com.xiaotu.spider.maoyan.pageprocessor.MaoYanMovieBasicInfoPageProcessor;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Html;
/***
 * 骨朵筛选出网大数据从猫眼爬取其基本信息
 * @author zhang
 *
 */
@Component("GuDuoTVBasicInfoPageProcessor")
public class GuDuoTVBasicInfoPageProcessor extends MaoYanMovieBasicInfoPageProcessor {
	
	@Autowired
	private TVDataSourceRelationService tvDataSourceRelationService;
	
	protected Integer tvType = 1;//剧类型 :电视剧
	
	protected String resourceSiteId = Constants.SiteName.MAOYAN;//数据来源
	
	protected String paramResourceSiteId = Constants.SiteName.GUDUO;//数据来源
	
	protected static Map<String,Object> paramMap = null;
	
	{
		
		paramMap = new HashMap<String,Object>();
		paramMap.put("tvType", tvType);
		paramMap.put("paramResourceSiteId", paramResourceSiteId);
	}
	
	@Autowired
	private SupplementGuDuoRecordService supplementGuDuoRecordService;
		
	@Autowired
    private MaoYanMovieBasicInfoService maoYanMovieBasicInfoService;
	
	private String [] start_url = {"https://www.baidu.com/"};
	
	private String business_url = "https://piaofang.maoyan.com/search?key=%s";
	
	
	@Override
	public void process(Page page) {
		
		Map<String, Object> extrasMap = page.getRequest().getExtras();
		
		switch (this.getPageDataType(page))
        {
            case "search":
                this.matchTvProcess(page,extrasMap);
                break;
            case Constants.PageDataType.BASICINFO:
                super.basicinfoProcess(page,extrasMap);
                break;
            case "_v_=yes":
            	this.basicinfoOtherProcess(page,extrasMap);
            	break;
            case "/companylist":
	            super.basicinfoCompanylistProcess(page,extrasMap);
	            break;
            case "/celebritylist":
            	super.basicinfoPeoplelistProcess(page,extrasMap);
            	break;
            case "/celebrity?id":
            	super.starBasicinfoProcess(page,extrasMap);
            	break;
            case "/companydetail":
            	super.companyBasicinfoProcess(page,extrasMap);
            	break;
            case "/artist":
            	super.companySignActorProcess(page,extrasMap);//签约艺人
            	break;	
            default:
            	this.addRequestProcess(page);
            	break;
        }
		
	}
	
	//网剧基本信息
	public void basicinfoOtherProcess(Page page, Map<String, Object> extrasMap){
		
		MaoYanMovieBasicInfoModel maoYanMovieBasicInfoModel = (MaoYanMovieBasicInfoModel) extrasMap.get("movieBasicInfo");
		
		//骨朵剧名称
		String gu_tvName = extrasMap.get("gu_tvName")==null?null:extrasMap.get("gu_tvName").toString();
		
		//骨朵剧的网站数据id
		String gu_dataId = extrasMap.get("gu_dataId")==null?null:extrasMap.get("gu_dataId").toString();
		
		//猫眼网站数据id
		String maoy_dataId = extrasMap.get("maoy_dataId")==null?null:extrasMap.get("maoy_dataId").toString();
		
		//猫眼网站剧名
		String maoy_tvName = extrasMap.get("maoy_tvName")==null?null:extrasMap.get("maoy_tvName").toString();
		
		
		String url = page.getRequest().getUrl();
		
		String dataId = extrasMap.get("dataId").toString();
		
		if(url.contains(dataId)){
			//类型
			String [] label = null;
			String labelStr = page.getHtml().xpath("//div[@class='tv-container']//div[@class='info-detail']//div[@class='info-detail-extra']//div[@class='info-list']//p//span[@class='tv-types']").get();
			if(null != labelStr){
				labelStr = HtmlUtil.delHTMLTag(labelStr).replaceAll("[ ]", "").replaceAll("[/,，]",",");
				if(StringUtils.isBlank(labelStr)){
					label = null;
				}else{
					label = labelStr.split(",");
				}
			}
			//时长
			String timeLength = null;
			//集数
			String sets = null;
			//上映日期
			String showingDate = null;
			List<String> pTagList = page.getHtml().xpath("//div[@class='tv-container']//div[@class='info-detail']//div[@class='info-detail-extra']//p").all();
			if(null != pTagList && pTagList.size()>0){
				for(String pTag : pTagList){
					Html pTagHtml = new Html(pTag);
					
					if(null != pTagHtml.xpath("//p/@class") && pTagHtml.xpath("//p/@class").get().contains("text")){
						List<String> spanList = pTagHtml.xpath("//p//span").all();
						
						for(String span : spanList){
							Html spanHtml = new Html(span);
							String spanStr =  spanHtml.xpath("//span/text()").get();
							if(spanStr.contains("集数")){
								sets = MathUtil.getNumber(spanStr);
								continue;
							}
							if(spanStr.contains("分钟")){
								timeLength = MathUtil.getNumber(spanStr);
								continue;
							}
							if(spanStr.lastIndexOf("上线")!=-1 && !(spanStr.lastIndexOf("已上线")!=-1)){
								showingDate = spanStr.substring(0,spanStr.indexOf("上线"));
								showingDate = showingDate.replaceAll("[\u4e00-\u9fa5-]", "");
								if(showingDate.length()==8){
									showingDate = super.convertDateStr(showingDate);
								}
							}
						}
						
					}
					
				}
			}
			
			//首先判断是否是网剧（3）
			String netMovieFlag = page.getHtml().xpath("//div[@class='tv-baseinfo']").get();
			if(null != netMovieFlag){
				maoYanMovieBasicInfoModel.setLabel(label);
				maoYanMovieBasicInfoModel.setTimeLength(timeLength);
				maoYanMovieBasicInfoModel.setShowingDate(showingDate);
				maoYanMovieBasicInfoModel.setSets(sets);
				extrasMap.put("movieBasicInfo", maoYanMovieBasicInfoModel);
				extrasMap.put("dataId", maoy_dataId);
				super.basicinfoOtherProcess(page, extrasMap);
				//首先判断之前是否存在记录
				TVDataSourceRelatetionModel model = tvDataSourceRelationService.queryTvDataSourceRelation(extrasMap);
				if(null == model){
					//插入剧数据源关系表中
					new AsyncProcess(new TVDataSourceRelatetionModel(
							null,//基础库剧id暂时没有
							gu_dataId,
							gu_tvName,
							maoy_dataId,//猫眼id
							maoy_tvName,//猫眼剧名
							null,//爱奇艺id
							null,//爱奇艺剧名
							System.currentTimeMillis()/1000
							)).asyncMethod();
				}else{
					//判断猫眼数据id是否为空
					if(null == model.getMaoy_id() || StringUtils.isBlank(model.getMaoy_id())){
						//修改
						new AsyncProcess(new TVDataSourceRelatetionModel(
								null,//基础库剧id暂时没有
								gu_dataId,
								gu_tvName,
								maoy_dataId,//猫眼id
								maoy_tvName,//猫眼剧名
								model.getGd_id(),//爱奇艺id
								model.getGd_name(),//爱奇艺剧名
								System.currentTimeMillis()/1000
								)).asyncUpdMethod();
					}
				}
			}else{
				//首先判断之前是否存在记录
				TVDataSourceRelatetionModel model = tvDataSourceRelationService.queryTvDataSourceRelation(extrasMap);
				if(null == model){
					//插入剧数据源关系表中
					new AsyncProcess(new TVDataSourceRelatetionModel(
							null,//基础库剧id暂时没有
							gu_dataId,
							gu_tvName,
							null,//猫眼id
							null,//猫眼剧名
							null,//爱奇艺id
							null,//爱奇艺剧名
							System.currentTimeMillis()/1000
							)).asyncMethod();
				}
			}
			
		}
	}
	
	
	//将骨朵影视剧与猫眼影视剧进行匹配
	@SuppressWarnings("unused")
	private void matchTvProcess(Page page, Map<String, Object> extrasMap) {
		
		//骨朵剧名称
		String gu_tvName = extrasMap.get("gu_tvName")==null?null:extrasMap.get("gu_tvName").toString();
		//猫眼剧上映时间
		String gu_tvRelaseDate = extrasMap.get("relaseDate")==null?null:extrasMap.get("relaseDate").toString().replaceAll("[- ]", "");
		//猫眼剧导演
		String gu_director = extrasMap.get("director")==null?null:extrasMap.get("director").toString().replaceAll("[,，]", "/");
		//猫眼剧主演
		String gu_actor = extrasMap.get("actors")==null?null:extrasMap.get("actors").toString().replaceAll("[,，]", "/");
		//骨朵剧的网站数据id
		String gu_dataId = extrasMap.get("gu_dataId")==null?null:extrasMap.get("gu_dataId").toString();
		
		
		List<String> articleTagList = page.getHtml().xpath("//div[@id='search-list']//article").all();
		
		List<Map<String,Object>> appearanceMapList = new ArrayList<Map<String,Object>>();
		
		//正确率最高的
		String maxAppearanceMaoDataId = "";
		String maxAppearance = "";
		String maxAppearanceTvname = "";
		
		for(String article : articleTagList){
			
			Map<String,Object> appearanceMap = new HashMap<String,Object>();
			
			Html articleHtml = new Html(article);
			//猫眼剧名称
			String mao_tvName = null;
			//猫眼剧上映时间
			String mao_tvRelaseDate = null;
			//猫眼剧导演
			String mao_director = null;
			//猫眼剧主演
			String mao_actor = null;
			//猫眼剧的网站数据id
			String mao_dataId = null;
			
			//猫眼剧名称
			mao_tvName = articleHtml.xpath("//div[@class='title']//text()").get().trim();
			//猫眼剧上映时间
			if(null != articleHtml.xpath("//p//text()").all().get(0)){
				mao_tvRelaseDate = articleHtml.xpath("//p//text()").all().get(0).trim().replaceAll("[\u4e00-\u9fa5- ]", "");
			}
			
			//猫眼剧导演
			if(null != articleHtml.xpath("//p[@class='lineDot']//span[@class='movie-director']//text()").get()){
				mao_director = convertStr(articleHtml.xpath("//p[@class='lineDot']//span[@class='movie-director']//text()").get().trim());
			}
			
			//猫眼剧主演
			if(null == mao_director && null != articleHtml.xpath("//p[@class='lineDot']//span//text()").all().get(0)){
				mao_actor = convertStr(articleHtml.xpath("//p[@class='lineDot']//span//text()").all().get(0).trim());
			}
			if(null != mao_director && null != articleHtml.xpath("//p[@class='lineDot']//span//text()").all().get(1)){
				mao_actor = convertStr(articleHtml.xpath("//p[@class='lineDot']//span//text()").all().get(1).trim());
			}
			
			//猫眼剧的网站数据id
			if(null != articleHtml.xpath("//article[@class='indentInner canTouch']/@data-url").get()){
				mao_dataId = articleHtml.xpath("//article[@class='indentInner canTouch']/@data-url").get().replaceAll("[a-z/]", "");
			}
			
			
			int appearance = 0;
			//剧名
			if(StringUtils.isNotBlank(mao_tvName) && StringUtils.isNotBlank(gu_tvName)){
				if(StringUtil.stringFilter(mao_tvName).equals(StringUtil.stringFilter(gu_tvName))){
					appearance++;
					
					//上映时间
					if(StringUtils.isNotBlank(gu_tvRelaseDate) && StringUtils.isNotBlank(mao_tvRelaseDate)
							&& gu_tvRelaseDate.equals(mao_tvRelaseDate)){
						appearance++;
					}
					//导演
					if(StringUtils.isNotBlank(gu_director) && StringUtils.isNotBlank(mao_director)
							&& (isContains(mao_director,gu_director) || isContains(gu_director,mao_director))){
						appearance++;
					}
					//演员
					if(StringUtils.isNotBlank(gu_actor) && StringUtils.isNotBlank(mao_actor)
							&& (isContains(mao_actor,gu_actor) || isContains(gu_actor,mao_actor))){
						appearance++;
					}
					
				}
			}
			
			appearanceMap.put("mao_dataId", mao_dataId);
			appearanceMap.put("appearance", appearance);
			appearanceMap.put("mao_tvName", mao_tvName);
			appearanceMapList.add(appearanceMap);
		}
		
		if(appearanceMapList.size()>0){
			Collections.sort(appearanceMapList, new Comparator<Map<String,Object>>() {

				@Override
				public int compare(Map<String, Object> o1, Map<String, Object> o2) {
					
					int v1 = Integer.parseInt(o1.get("appearance").toString()); 
					int v2 = Integer.parseInt(o2.get("appearance").toString());
					
						if((v2-v1)>0){
							return 1;
						}else if((v2-v1)<0){
							return -1;
						}else{
							return 0;
						}
				}
			});
			
			//正确率最高的
			maxAppearanceMaoDataId = appearanceMapList.get(0).get("mao_dataId").toString();
			maxAppearance = appearanceMapList.get(0).get("appearance").toString();
			maxAppearanceTvname = appearanceMapList.get(0).get("mao_tvName").toString();
			
			int appearance = Integer.parseInt(appearanceMapList.get(0).get("appearance").toString());
			if(appearance>=1){//最低条件名字匹配
				maxAppearanceMaoDataId = appearanceMapList.get(0).get("mao_dataId").toString();
				if(StringUtils.isNotBlank(maxAppearanceMaoDataId)){
					//爬取猫眼其他相关信息
					Map<String,Object> paramsMap = new HashMap<String,Object>();
					paramsMap.put("maoy_dataId", maxAppearanceMaoDataId);
					paramsMap.put("maoy_tvName", maxAppearanceTvname);
					paramsMap.put("filedName", "gd_id");
					paramsMap.put("resourceSiteId", Constants.SiteName.MAOYAN);
					paramsMap.put("gu_dataId", gu_dataId);
					paramsMap.put("gu_tvName", gu_tvName);
					/***
					 * 误删
					 */
					paramsMap.put("dataId", maxAppearanceMaoDataId);
					paramsMap.put("movieName", maxAppearanceTvname);
					
					
					//爬取猫眼
					MaoYanMovieBasicInfoModel movieBasicInfo = maoYanMovieBasicInfoService.queryMovieBasicInfoList(paramsMap);
					if(null == movieBasicInfo){
						super.tvType = tvType;
						super.resourceSiteId = resourceSiteId;
						PageUtils.addTargetRequest(page, String.format("https://piaofang.maoyan.com/movie/%s/moresections", maxAppearanceMaoDataId), paramsMap);
					}
				}
			}else{
				//并且不存在表中
				TVDataSourceRelatetionModel model = tvDataSourceRelationService.queryTvDataSourceRelation(extrasMap);
				if(null == model){
					//暂未匹配到猫眼对应的剧
					new AsyncProcess(new TVDataSourceRelatetionModel(
							null,//基础库剧id暂时没有
							gu_dataId,
							gu_tvName,
							null,//猫眼id
							null,//猫眼剧名
							null,//爱奇艺id
							null,//爱奇艺剧名
							System.currentTimeMillis()/1000
							)).asyncMethod();
				}
				
			}
		}else{
			//猫眼中不存在该剧
			TVDataSourceRelatetionModel model = tvDataSourceRelationService.queryTvDataSourceRelation(extrasMap);
			if(null == model){
				//暂未匹配到猫眼对应的剧
				new AsyncProcess(new TVDataSourceRelatetionModel(
						null,//基础库剧id暂时没有
						gu_dataId,
						gu_tvName,
						null,//猫眼id
						null,//猫眼剧名
						null,//爱奇艺id
						null,//爱奇艺剧名
						System.currentTimeMillis()/1000
						)).asyncMethod();
			}
		}
		
	}

	
	private boolean isContains(String str1,String str2){
		
		boolean flag = false;
		String[] str1Arr = str1.split("/");
		String[] str2Arr = str2.split("/");
		for(String s1 : str1Arr){
			for(String s2 : str2Arr){
				if(s1.contains(s2)){
					flag = true;
					return flag;
				}
			}
		}
		
		return flag;
	}
	
	
	private String convertStr(String paramStr){
		
		if(paramStr.length()<=0){
			return null;
		}
		if(StringUtils.isNotBlank(paramStr)){
			return paramStr.replaceAll("[导演 演员：]", "").replaceAll("[，,]", "/");
		}
		return null;
	}
	
	
	// 用于添加请求
	@Override
	public void addRequestProcess(Page page) {
		
		List<TvInfoSpiderTempModel> list = supplementGuDuoRecordService.queryTvInfoSpiderTempDataIsNoSupplementList(paramMap);
		if (null != list && list.size() > 0) {
			for (TvInfoSpiderTempModel obj : list) {

				String gu_dataId = obj.getTv_dataid();
				String tvName = obj.getTvname();
				String actors = obj.getActors();
				String movieType = obj.getLabels();
				String director = obj.getDirectors();
				String relaseDate = obj.getRelasedate();

				Map<String, Object> extraMap = new HashMap<String, Object>();
				extraMap.put("gu_dataId", gu_dataId);
				extraMap.put("gu_tvName", tvName);
				extraMap.put("actors", actors);
				extraMap.put("movieType", movieType);
				extraMap.put("director", director);
				extraMap.put("relaseDate", relaseDate);
				extraMap.put("filedName", "gd_id");//骨朵
				if (StringUtils.isBlank(tvName))
					return;
				PageUtils.addTargetRequest(page, String.format(business_url, KeywordEncode.encode(tvName)), extraMap);
			}
		}
	}

	
	@Override
	protected String[] getStartRequestUrls() {
		return start_url;
	}

	@Override
	protected String getSpiderType() {
		return Constants.SpiderTaskType.MOVIE_BOXOFFICE;
	}

	@Override
	public String getPageDataType(Page page) {
		String url = page.getRequest().getUrl();
		if(url.contains("/search?key")){
			return "search";
		}else if (url.contains("moresections")){
        	return Constants.PageDataType.BASICINFO;
        }else if (url.contains("_v_=yes")){
        	return "_v_=yes";
        }else if (url.contains("/companylist")){
        	return "/companylist";
        }else if (url.contains("/celebritylist")){
			return "/celebritylist";
		}else if(url.contains("/celebrity?id")){
			return "/celebrity?id";
		}else if(url.contains("/companydetail")){
			String tempUrl = url.substring(url.lastIndexOf("/")+1).replaceAll("[0-9]","");
			if(tempUrl.equals("")){
				return "/companydetail";
			}else if(tempUrl.equals("artist")){
				return "/artist";
			}
		}
		return "addRequest";
	}
	
}
